function PopupBox2() {
  this._background = null;
  this._container = null;
}

PopupBox2.prototype._reset = function(pos) {
  this._close();
  
  this._background = document.createElement('div');
  this._background.className = 'popupWhite';
  this._background.style.width = document.body.offsetWidth + 'px';
  this._background.style.height = document.body.offsetHeight + 'px';
  document.body.appendChild(this._background);
  
  this._container = document.createElement('div');
  this._container.className = 'popupBase1';
  this._container.style.top = ((this._position[1] - 60) > 0) ? (this._position[1] - 60) + 'px' : '0px';
  this._container.style.left = ((this._position[0] + 200) > window.screen.width) ? (window.screen.width - 200) + 'px' : this._position[0] + 'px';
  this._background.appendChild(this._container);
  
  var posX = this._position[0];
  var posY = ((this._position[1] - 100) > 0) ? (this._position[1] - 100) : 0;
  window.scrollTo(posX, posY);
};

PopupBox2.prototype._open = function(container, options) {
  var table, tr, td, _self = this;
  
  this._title = (options && options.t) ? options.t : '';
  
  this._position = (options && options.pos) ? options.pos : [((document.body.offsetWidth / 2) - 50), ((document.body.offsetHeight / 2) - 50)];
  
  this._reset();
  
  this._gui = new PopupBox2Gui(this._container, null, options)._gui;
  this._callbackFunc = (options && options.func) ? options.func : null;
  
  this._gui.title.appendChild(document.createTextNode(this._title));
  this._gui.close.onclick = function() { _self._close(); 
                                         if (_self._callbackFunc) {
                                           _self._callbackFunc();
                                         }
                                       };
  this._gui.panel.appendChild(container);

  this._moveX = 0;
  this._moveY = 0;
  this._moveTop = 0;
  this._moveLeft = 0;
  this._moveable = false;
  this._docMouseMoveEvent = document.onmousemove;
  this._docMouseUpEvent = document.onmouseup;
  this._iWidth = document.body.clientWidth;
  this._iHeight = document.body.clientHeight;
  this._width = (options && options.width) ? options.width : 513;
  
  this._gui.menu.onmouseover = function() { this.className = 'popupMove'; };
  this._gui.menu.onmousedown = function() { var evt = _self._getEvent();
                                            _self._moveable = true;
                                            _self._moveX = evt.clientX;
                                            _self._moveY = evt.clientY;
                                            _self._moveTop = parseInt(_self._container.style.top);
                                            _self._moveLeft = parseInt(_self._container.style.left);
                                          };
  this._gui.menu.onmousemove = function() { if (_self._moveable) {
                                              this.className = 'popupMove';
                                              var evt = _self._getEvent();
                                              var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                              var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                              if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              } else {
                                                _self._container.style.left = x + "px";
                                                _self._container.style.top = y + "px";
                                              }
                                            }
                                          };
  this._gui.menu.onmouseup = function () { if (_self._moveable) {
                                             document.onmousemove = _self._docMouseMoveEvent;
                                             document.onmouseup = _self._docMouseUpEvent;
                                             _self._moveable = false;
                                             _self._moveX = 0;
                                             _self._moveY = 0;
                                             _self._moveTop = 0;
                                             _self._moveLeft = 0;
                                             this.className = 'popupUp';
                                           }
                                         };
  
  document.body.onmousemove = function() { if (_self._moveable) {
                                             var evt = _self._getEvent();
                                             var x = parseInt(_self._moveLeft) + parseInt(evt.clientX) - parseInt(_self._moveX);
                                             var y = parseInt(_self._moveTop) + parseInt(evt.clientY) - parseInt(_self._moveY);
                                             
                                             if ( x > 0 && ( x + _self._width < _self._iWidth) && y > 0 && (y + 300 < _self._iHeight) ) {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             } else {
                                               _self._container.style.left = x + "px";
                                               _self._container.style.top = y + "px";
                                             }
                                           }
                                         };
  
  document.body.onmouseup = function () { if (_self._moveable) {
                                            document.onmousemove = _self._docMouseMoveEvent;
                                            document.onmouseup = _self._docMouseUpEvent;
                                            _self._moveable = false;
                                            _self._moveX = 0;
                                            _self._moveY = 0;
                                            _self._moveTop = 0;
                                            _self._moveLeft = 0;
                                            this.className = 'popupUp';
                                          }
                                        };
};

PopupBox2.prototype._close = function() {
  if (this._background) {
    document.body.removeChild(this._background);
    this._background = null;
    this._container = null;
  }
};


PopupBox2.prototype._getEvent = function() {
  if(window.parent == window) {
    return window.event || arguments.callee.caller.arguments[0];
  } else {
    return window.parent.event || arguments.callee.caller.arguments[0];
  }
};
